package com.leoman.userapi.service.impl;

import com.leoman.common.service.impl.GenericManagerImpl;
import com.leoman.entity.Constant;
import com.leoman.reourceapi.service.impl.ResourceApiServiceImpl;
import com.leoman.user.dao.UserInfoDao;
import com.leoman.user.dao.UserIntegralDao;
import com.leoman.user.entity.UserInfo;
import com.leoman.user.entity.UserIntegral;
import com.leoman.userapi.service.UserIntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户积分计算实现
 * Created by tutu on 2016/6/12.
 */
@Service
public class UserIntegralServiceImpl extends GenericManagerImpl<UserIntegral, UserIntegralDao> implements UserIntegralService {

    @Autowired
    private UserIntegralDao userIntegralDao;

    @Autowired
    private UserInfoDao userInfoDao;

    @Override
    @Transactional
    public void changeIntegral(UserInfo userInfo, String content, Integer count) {
        UserIntegral userIntegral = new UserIntegral();
        userIntegral.setUserInfo(userInfo);
        userIntegral.setContent(content);
        userIntegral.setCount(count);
        userIntegral.setType(Constant.USER_INTEGRAL_CHANGE_TYPE_001);
        if ("馒头兑换经验值".equals(content)) {
            userIntegral.setActionType(1);
        }
        if ("打赏馒头".equals(content)) {
            userIntegral.setActionType(2);
        }
        if ("邀请好友成功注册".equals(content)) {
            userIntegral.setActionType(3);
        }
        userIntegralDao.save(userIntegral);

        // 更新用户积分数
        userInfo.setIntegral(userInfo.getIntegral() + count);
        userInfo.setAllIntegral(userInfo.getAllIntegral() + count);

        // 更新用户等级
        if (userInfo.getIntegral() < 151) {
            userInfo.setLevel(1);
        } else if (userInfo.getIntegral() < 301) {
            userInfo.setLevel(2);
        } else if (userInfo.getIntegral() < 451) {
            userInfo.setLevel(3);
        } else if (userInfo.getIntegral() < 751) {
            userInfo.setLevel(4);
        } else if (userInfo.getIntegral() < 1351) {
            userInfo.setLevel(5);
        } else if (userInfo.getIntegral() < 2251) {
            userInfo.setLevel(6);
        } else if (userInfo.getIntegral() < 3451) {
            userInfo.setLevel(7);
        } else if (userInfo.getIntegral() < 4951) {
            userInfo.setLevel(8);
        } else if (userInfo.getIntegral() < 6751) {
            userInfo.setLevel(9);
        } else if (userInfo.getIntegral() < 8551) {
            userInfo.setLevel(10);
        } else if (userInfo.getIntegral() < 10351) {
            userInfo.setLevel(11);
        } else if (userInfo.getIntegral() < 12451) {
            userInfo.setLevel(12);
        } else if (userInfo.getIntegral() < 14551) {
            userInfo.setLevel(13);
        } else if (userInfo.getIntegral() < 16651) {
            userInfo.setLevel(14);
        } else if (userInfo.getIntegral() < 19051) {
            userInfo.setLevel(15);
        } else if (userInfo.getIntegral() < 21450) {
            userInfo.setLevel(16);
        } else if (userInfo.getIntegral() < 23851) {
            userInfo.setLevel(17);
        } else {
            userInfo.setLevel(18);
        }

        userInfoDao.save(userInfo);
    }

    @Override
    @Transactional
    public void changeCoin(UserInfo userInfo, String content, Integer count) {
        // 增加馒头变动记录
        UserIntegral userIntegral = new UserIntegral();
        userIntegral.setUserInfo(userInfo);
        userIntegral.setCount(count);
        userIntegral.setType(Constant.USER_INTEGRAL_CHANGE_TYPE_002);
        userIntegral.setContent(content);

        if ("馒头兑换经验值".equals(content)) {
            userIntegral.setActionType(1);
        }
        if ("打赏馒头".equals(content)) {
            userIntegral.setActionType(2);
        }
        if ("邀请好友成功注册".equals(content)) {
            userIntegral.setActionType(3);
        }
        if ("被用户打赏馒头".equals(content)) {
            userIntegral.setActionType(4);
        }

        userIntegralDao.save(userIntegral);

        // 修改用户馒头数
        userInfo.setCoin(userInfo.getCoin() + count);

        userInfoDao.save(userInfo);
    }

    @Override
    public Page<UserIntegral> integralPage(Long userId, Integer type, Integer pageNum, Integer pageSize) {
        int[] info = ResourceApiServiceImpl.changePageParams(pageNum, pageSize);
        return userIntegralDao.pageByParams(userId, type, new PageRequest(info[0] - 1, info[1]));
    }

    @Override
    public Long getMoreIntegral(Long integral) {
        Long levelUpIntegral;

        if (0 <= integral && integral <= 150) {
            // lv1
            levelUpIntegral = 151 - integral;
        } else if (151 <= integral && integral <= 300) {
            // lv2
            levelUpIntegral = 301 - integral;
        } else if (301 <= integral && integral <= 450) {
            // lv3
            levelUpIntegral = 451 - integral;
        } else if (451 <= integral && integral <= 750) {
            // lv4
            levelUpIntegral = 751 - integral;
        } else if (751 <= integral && integral <= 1350) {
            // lv5
            levelUpIntegral = 1351 - integral;
        } else if (1351 <= integral && integral <= 2250) {
            // lv6
            levelUpIntegral = 2251 - integral;
        } else if (2251 <= integral && integral <= 3450) {
            // lv7
            levelUpIntegral = 3451 - integral;
        } else if (3451 <= integral && integral <= 4950) {
            // lv8
            levelUpIntegral = 4951 - integral;
        } else if (4951 <= integral && integral <= 6750) {
            // lv9
            levelUpIntegral = 6751 - integral;
        } else if (6751 <= integral && integral <= 8550) {
            // lv10
            levelUpIntegral = 8551 - integral;
        } else if (8551 <= integral && integral <= 10350) {
            // lv11
            levelUpIntegral = 10351 - integral;
        } else if (10351 <= integral && integral <= 12450) {
            // lv12
            levelUpIntegral = 12451 - integral;
        } else if (12451 <= integral && integral <= 14550) {
            // lv13
            levelUpIntegral = 14551 - integral;
        } else if (14551 <= integral && integral <= 16650) {
            // lv14
            levelUpIntegral = 16651 - integral;
        } else if (16651 <= integral && integral <= 19050) {
            // lv15
            levelUpIntegral = 19051 - integral;
        } else if (19051 <= integral && integral <= 21450) {
            // lv16
            levelUpIntegral = 21451 - integral;
        } else if (21451 <= integral && integral <= 23850) {
            // lv17
            levelUpIntegral = 23851 - integral;
        } else {
            // lv18
            levelUpIntegral = integral - 23851;
        }

        return levelUpIntegral;
    }
}
